package com.ita.shoppingcart.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.shoppingcart.bean.Goods;
import com.ita.shoppingcart.cart.CartManager;
import com.ita.shoppingcart.cart.CartManagerImpl;

/**
 * Servlet implementation class ListCartServlet
 */
public class ListCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CartManager cartManager = new CartManagerImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListCartServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		Map<Integer, Goods> cart = (Map<Integer, Goods>) session.getAttribute("cart");
		if(cart == null){
			cart = new HashMap<Integer, Goods>();
			session.setAttribute("cart", cart);
		}
		List<Goods> list = cartManager.showCart(cart);
		double total = cartManager.goodsTotal(cart);
		request.setAttribute("cartList", list);
		request.setAttribute("total", total);
		request.getRequestDispatcher("./jsp/listCart.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
